package com.azt.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.azt.model.KuaidiInfo;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alibaba.fastjson.JSON.toJavaObject;

/**
 * 快递100 API
 * Created by shangwq on 2016/9/26.
 */
public class KuaidiUtil {

    private static final String KEY = "ec03ea7c5ff9f985";

    private static final String API = "http://api.kuaidi100.com/api?id={0}&com={1}&nu={2}";

    /**
     * 获取物流信息
     * @param com 快递公司code
     * @param no 快递单号
     * */
    public static KuaidiInfo getInfo(String com, String no) {
        String url = MessageFormat.format(API, KEY, com, no);
        String result = HttpClientUtil.doGet(url);
        return parseResult(result);
    }

    /**
     * 解析api返回的结果
     * */
    private static KuaidiInfo parseResult(String result) {
        KuaidiInfo info = null;
        if(StringUtils.isNotBlank(result)) {
            JSONObject json = JSON.parseObject(result);
            if(json != null) {
                info = JSONObject.toJavaObject(json, KuaidiInfo.class);
            }
        }
        return info;
    }

}
